const {
  crateGoods,
  updateGoods,
  removeGoods,
  restoreGoods,
  destoreGoods,
  findGoodsAll,
} = require("../service/goods.service");

const path = require("path");

class Goods {
  upload(ctx, next) {
    console.log(ctx.request, "files");
    const { file } = ctx.request.files;
    console.log(file, "file");

    // console.log(file._writeStream.path,'file1');
    //path.basename("路径")  //拿到路径的最后一项
    // console.log(path.basename("C:/Users/lishuaihao/Desktop/学习文件夹及遇见的问题/Koa的搭建/src/uploads"),'base');
    console.log(path.basename(file._writeStream.path), "file2");
    ctx.body = {
      code: 0,
      msg: "上传成功",
      resule: {
        goods_img: path.basename(file._writeStream.path),
      },
    };
  }
  async create(ctx, next) {
    try {
      await crateGoods(ctx.request.body);
      ctx.body = {
        code: 0,
        msg: "添加成功",
      };
    } catch (e) {
      console.log(e, "e");
    }
  }
  async update(ctx, next) {
    console.log("-=-=");
    let res = await updateGoods(ctx.params.id, ctx.request.body);
    if (res) {
      ctx.body = {
        code: 0,
        msg: "修改成功",
      };
    }
  }
  async remove(ctx, next) {
    const res = await removeGoods(ctx.params.id);
    if (res) {
      ctx.body = {
        code: 0,
        msg: "删除成功",
      };
    }
  }
  async destore(ctx, next) {
    await destoreGoods(ctx.params.id);
    ctx.body = {
      code: 0,
      msg: "商品下架成功",
    };
  }
  async restore(ctx, next) {
    await restoreGoods(ctx.params.id);
    ctx.body = {
      code: 0,
      msg: "商品上架成功",
    };
  }
  async findAll(ctx, next) {
    const { pagesize, pagenum } = ctx.query;
    console.log(ctx.query.pagesize, ctx.query.pagenum, "params");
    const res = await findGoodsAll(pagesize, pagenum);
    ctx.body = {
      code: 0,
      pagesize,
      pagenum,
      total: res.count,
      list: res.list,
    };
  }
}

module.exports = new Goods();
